package com.jack.thinking.java._22.swing.jnlp;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.ByteArrayInputStream;
import java.io.IOException;

import javax.jnlp.FileContents;
import javax.jnlp.FileOpenService;
import javax.jnlp.FileSaveService;
import javax.jnlp.ServiceManager;
import javax.jnlp.UnavailableServiceException;
import javax.swing.*;

/**
 * 这个例子大致说了下Jnlp 是怎么回事，这里没法启动， 
 * 一种比applet 更好的方式，同样若没有签名需要通过沙盒运行， ServiceManager.lookup
 * @author wangbingwu
 *
 */
public class JnlpFileChooser extends JFrame{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField fileName = new JTextField();
	private JButton open = new JButton("Open");
	private JButton save = new JButton("Save");
	
	private JEditorPane ep = new JEditorPane();
	private JScrollPane jsp = new JScrollPane();
	private FileContents fileContents ;
	
	public JnlpFileChooser() {
		JPanel p = new JPanel();
		open.addActionListener(new OpenL());
		p.add(open);
		save.addActionListener(new SaveL());
		p.add(save);
		jsp.getViewport().add(ep);
		add(jsp, BorderLayout.CENTER);
		add(p, BorderLayout.SOUTH);
		fileName.setEditable(false);
		p = new JPanel();
		p.setLayout(new GridLayout(2, 1));
		p.add(fileName);
		add(p, BorderLayout.NORTH);
		ep.setContentType("text");
		save.setEnabled(false);
	}
	
	class OpenL implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			FileOpenService fs = null;
			try {
				fs = (FileOpenService) ServiceManager.lookup("javax.jnlp.FileOpenService");
			} catch (UnavailableServiceException e1) {
				e1.printStackTrace();
			}
			
			if(fs != null) {
				try {
					fileContents = fs.openFileDialog(".", new String[] {"txt", "*"});
					if(fileContents == null) {
						return;
					}
					fileName.setText(fileContents.getName());
					ep.read(fileContents.getInputStream(), null);
				} catch (IOException e1) {
					e1.printStackTrace();
				}
				save.setEnabled(true);
			}
		}
		
	}
	
	class SaveL implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			FileSaveService fs = null;
			try {
				fs = (FileSaveService) ServiceManager.lookup("javax.jnlp.FileSaveService");
			} catch (UnavailableServiceException e1) {
				e1.printStackTrace();
			}
			
			if(fs != null) {
				try {
					fileContents = fs.saveFileDialog(".", new String[] {"txt"}, new ByteArrayInputStream(ep.getText().getBytes()), fileContents.getName());
					if(fileContents == null) {
						return;
					}
					fileName.setText(fileContents.getName());
				} catch (IOException e1) {
					e1.printStackTrace();
				}
				save.setEnabled(true);
			}
		}
		
	}
	
	public static void main(String[] args) {
		JnlpFileChooser fc = new JnlpFileChooser();
		fc.setSize(400, 300);
		fc.setVisible(true);
	}
}
